package com.ygqh.baby.controller.mobile;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.model.YgProductCommentModel;
import com.ygqh.baby.service.YgProductCommentService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 商品类评论
 *
 * @author ZhangSai
 */
@Controller
@RequestMapping("/mobile/productComent")
public class YgProductComentController {

    @Autowired
    private YgProductCommentService ygProductCommentService;

    @ResponseBody
    @RequestMapping("list")
    public JSONPObject list(Long productId, QueryInfo queryInfo, Long groupId, String callback) {
        List<YgProductCommentModel> list = ygProductCommentService.findModelList(queryInfo, groupId, productId);
        return new JSONPObject(callback, Message.success(list));
    }

    @ResponseBody
    @RequestMapping("count")
    public JSONPObject count(Long productId, @RequestParam String callback) {
        int count = 0;
        if (productId != null) {
            count = ygProductCommentService.count(productId);
        }
        return new JSONPObject(callback, Message.success(count));
    }

    @ResponseBody
    @RequestMapping("commentCount")
    public JSONPObject commentCount(Long groupId, @RequestParam String callback) {
        int count = 0;
        if (groupId != null) {

            count = ygProductCommentService.countByCurrentGroup(groupId);
        }
        return new JSONPObject(callback, Message.success(count));
    }

    @ResponseBody
    @RequestMapping("commentList")
    public JSONPObject commentList(Long groupId, QueryInfo queryInfo, @RequestParam String callback) {
        List<YgProductCommentModel> findModelList = ygProductCommentService.findModelListByCurrentGroup(queryInfo, groupId);
        return new JSONPObject(callback, Message.success(findModelList));
    }

    @ResponseBody
    @RequestMapping("commentListByProductCode")
    public JSONPObject commentListByProductCode(QueryInfo queryInfo, String productCode, boolean showAll, @RequestParam String callback) {
        List<YgProductCommentModel> findModelList = ygProductCommentService.findModelListByProductCode(queryInfo, productCode, showAll);
        return new JSONPObject(callback, Message.success(findModelList));
    }

    @ResponseBody
    @RequestMapping("commentCountByProductCode")
    public JSONPObject commentCountByProductCode(String productCode, boolean showAll, @RequestParam String callback) {
        int count = 0;
        if (StringUtils.isNotBlank(productCode)) {
            count = ygProductCommentService.countByProductCode(productCode, showAll);
        }
        return new JSONPObject(callback, Message.success(count));
    }

}
